// pages/selected/selected.js
const app = getApp()
const http = require("../../config/api.js");
const user = require("../../utils/login.js");
Page({
  data: {
    imgUrl: "",
    isLogin: "",
    isVIP: "",
    query: {
      userId: "",
      code: "xcx_lhy1.0_jx",
      province: "",
      area: "",
      px: "",
      project: "lyhad",
      province: "",
      area: "",
      count: 10
    },
    images: [],
    layrecs: [],
    localHot:[],
    cur: 1, // 当前页数
  },
  onShow:function() {
    this.setData({
      userId: app.globalData.userId,
      imgUrl: app.globalData.baseUrlImg,
      isLogin: app.globalData.isLogin,
      isVIP: app.globalData.isVIP
    })
    this.fnGetSelected();
    this.fnGetEDayRMD();
    this.fnLocalHotOpera(app.globalData.userId);
    let time = Math.round(new Date().getTime());
    let log = {
      //日志类型：unite.通用业务；visit.访问数据；click.点击数据；order.订购数据；error.错误日志
      logType: "visit",
      msg: JSON.stringify({
        mac: app.globalData.userId,
        page: "xcxjx",
        pageName: "梨园行精选",
        time: time,
        appVersionName: "小程序",
        item: 65,
        nodeCode: "001000",
        project: "lyhad",
        userId: app.globalData.memberId,
        deviceId: app.globalData.userId,
        uuid: user.getUuid(),
        pageLevel: "1"
      })
    }
    this.uniteLog(log)
  },
  // 轮播图
  fnGetSelected() {
    let data = this.data.query;
    data.code = "xcx_lhy1.0_jx"
    http.lhxRequest("api_get", data).then(res => {
      if (res.code === 10000000) {
        this.setData({
          images: res.page.dynrecs
        })
      }
    })
  },
  //每日推荐
  fnGetEDayRMD() {
    let data = this.data.query;
    data.code = "ad_lyh2.0_home"
    http.lhxRequest("api_get", data).then(res => {
      if (res.code === 10000000) {
        console.log(res.page.layrecs, 1111111)
        this.setData({
          layrecs: res.page.layrecs
        })
      }
    })
  },
  // 当地热剧、猜你喜欢
  fnLocalHotOpera(userId) {
    let data = {
      userId: app.globalData.memberId ||userId,
      project: this.data.query.project,
      cur: this.data.cur,
      pageSize: this.data.query.count,
    }
    http.lhxRequest("api_recommand", data).then(res => {
      if (res.code === 10000000) {
        console.log(res)
        this.setData({
          localHot: res.data
        })
      }
    })
  },
  // 跳到详情
  fnJumpDetail(e) {
    const code = e.currentTarget.dataset.code;
    console.log(code)
    wx.navigateTo({
      url: "/pages/album_details/album_details?code=" + code,
    })
  },
  // 跳到详情
  fnJumpDetails(e) {
    const item = e.currentTarget.dataset.item;
    console.log(item)
    if (item.eleType == 'link') {
      let url = item.eleValue || '';
      if(item.eleValue.substr(0, 5) == 'https' || item.eleValue.substr(0, 4) == 'http'){
        wx.navigateTo({
          url: '/pages/webView/webView'
        })
      }else {
        wx.navigateTo({
          url: '/pages/webView/webView?url=' + encodeURIComponent(url)
        })
      }
    }else {
      wx.navigateTo({
        url: "/pages/album_details/album_details?code=" + item.eleValue,
      })
    }
  },
  // 日志
  uniteLog(data) {
    http.lhxRequest("api_log", data).then(res => {
      if (res.code === 10000000) {
        console.log("日志")
      }
    })
  }
})